package gmusic.command;

import gmusic.ma.bo.ArtistMABean;
import gmusic.ma.services.impl.MetalArchivesServicesImpl;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;

public class RechercherMAArtist implements Command {

	private final static Logger log = Logger
			.getLogger(RechercherMAArtist.class);

	public String execute(HttpServletRequest req) throws Exception {

		String name = req.getParameter("name");
		List<ArtistMABean> artists = MetalArchivesServicesImpl.getInstance()
				.searchArtistsFromMAWebSite(name);

		log.debug("Nb artists trouves = " + artists.size());
		if (artists.size() == 1) {
			log.debug("Artist = " + artists.get(0));
			MetalArchivesServicesImpl.getInstance()
					.persistArtistAndAllReleasesAndTracks(artists.get(0));
			return new AfficherArtistsMA().execute(req);
		}

		HttpSession session = req.getSession();
		session.setAttribute("artists", artists);

		return "/afficherResultatRechercheMAArtists.jsp";
	}
}
